#! /usr/bin/env python
# -*- coding: utf-8 -*-
# $Id :  $



# Copyright 2008-2010, Pascal Fautrero & JCharles
#
# This file is part of PyArchiver.
# 
# PyArchiver is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyArchiver is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PyArchiver.  If not, see <http://www.gnu.org/licenses/>./>.



############################################################################
# About     :  PyArchiver was built using Python 2.7,                      #
#              wxPython 2.8.11.0 unicode, and wxWidgets                    #
# Platform  :  Tested on Windows XP or Seven, Linux Ubuntu and Mac OSX     #
# Comment   :  To test the MySQL database, please install WampServer       #
#              --------->    WampServer is a local server    <--------     #
#              ---------->    http://www.wampserver.com    <----------     #
#              (available for Windows, linux and MacOSX (multilanguage)    #
############################################################################


# Import packages

import wx                        # This module uses the new wx namespace
import sys
import os
import platform

import version_info

import menu_bar
import status_bar
import task_bar_icon

from   wx.lib.buttons            import GenBitmapTextButton
import list_ctrl_virtual
import search_ctrl

import warning_dialog
import save_dialog
import password_dialog
import setting_dialog

import memento_provider
import about_notebook

import class_my_sql    

import print_framework
import scrolled_window
import ConfigParser              # Used to exploit .ini files
import hashlib

# import images                  # For PyEmbeddedImage

# Global Variables
#blah = 0
#blah = 1

_ = wx.GetTranslation            # Define _ or add _ to builtins in your app file

appDir = os.path.split(os.path.abspath(sys.argv[0]))[0]

#---------------------------------------------------------------------------
        
class My_Frame(wx.Frame):
    def __init__(self, parent, id, title):  
        wx.Frame.__init__(self, parent, -1, title)
        
        # Bind some events to an events handler
        self.Bind(wx.EVT_CLOSE, self.on_close_window)
        self.Bind(wx.EVT_ICONIZE, self.on_iconfiy)
        self.Bind(wx.EVT_MAXIMIZE, self.on_maximize)
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.on_erase_background)

        #-------------------------------------------------------------------

        # Global variables       
        self.shell = None
        self.isRolled = False
        self.isMemento = False
        self.isAbout = False
        
        #-------------------------------------------------------------------

        # A panel is needed for tab-traversal and platform-independent
        # background color capabilities.        
        self.panel = wx.Panel(self, id=-1, style=wx.TAB_TRAVERSAL)
        
        # Bind the enter window events to an events handler
        self.panel.Bind(wx.EVT_ENTER_WINDOW, self.on_panel_info, id=-1)

        #-------------------------------------------------------------------
        
        # Simplified init method
        self.load_database()
        self.create_menu_bar()
        self.create_status_bar()
        self.create_task_bar_icon()
        self.create_static_text()
        self.create_list_ctrl()
        self.populate_list_ctrl()
        self.create_search_ctrl()
        self.create_print_data()
        self.create_btn_bar()
        self.create_image()
        self.do_layout()
        
        #-------------------------------------------------------------------

        # Initialize to the current state
        self.unrolledFrameClientSize_size = self.GetClientSize()
        self.isRolled = False   # Initialize to the current state

        #-------------------------------------------------------------------
        
        # Restore frame state (pos and size)
        wx.CallAfter(self.restore_state)

        # Event Handlers
        self.Bind(wx.EVT_CLOSE, self.save_state)

    #-----------------------------------------------------------------------

    def load_database(self):
        self.dbObject = class_my_sql.DataBaseObject("basearchiver", "tablearchiver")

    #-----------------------------------------------------------------------
        
    def create_menu_bar(self):
        self.menu = menu_bar.My_MenuBar(self)
        self.SetMenuBar(self.menu)
        
        # Bind some menus events to an events handler
        self.Bind(wx.EVT_MENU, self.on_save, id=500)
        self.Bind(wx.EVT_MENU, self.on_print, id=501)
        self.Bind(wx.EVT_MENU, self.on_close, id=502)

        self.Bind(wx.EVT_MENU, self.on_delete, id=503)
        self.Bind(wx.EVT_MENU, self.on_setting, id=504)

        self.Bind(wx.EVT_MENU, self.on_roll, id=505)
        self.Bind(wx.EVT_MENU, self.on_unroll, id=506)
        self.Bind(wx.EVT_MENU, self.on_shell_window, id=507)
        self.Bind(wx.EVT_MENU, self.on_widget_inspector, id=508)
        
        self.Bind(wx.EVT_MENU, self.on_memento, id=509)
        self.Bind(wx.EVT_MENU, self.on_about, id=510)
        
    #-----------------------------------------------------------------------
        
    def create_status_bar(self):
        self.sb = status_bar.My_CustomStatusBar(self)
        self.SetStatusBar(self.sb)
        
    #-----------------------------------------------------------------------
        
    def create_task_bar_icon(self):
        self.tskic = task_bar_icon.My_TaskBarIcon(self)
                
    #-----------------------------------------------------------------------
        
    def create_static_text(self):
        # Create some StaticText
        self.text1 = wx.StaticText(self.panel, id=-1, label=_("Manual selection :"))
        font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
        font.SetWeight(wx.BOLD)
        self.text1.SetFont(font)
        self.text1.SetForegroundColour("#646464")
        
        # === Warning : self.text2 and text3 are used in list_ctrl_virtual.py
        self.panel1 = wx.Panel(self.panel, id=-1, style=wx.BORDER_SIMPLE)
        self.panel1.SetBackgroundColour("#fefed9")		
        self.text2 = wx.StaticText(self.panel1, id=-1, label="",
                                   style=wx.ALIGN_RIGHT | wx.ST_NO_AUTORESIZE)
        self.text2.SetForegroundColour("#68b1d8")
        
        # === Warning : self.text2 and text3 are used in list_ctrl_virtual.py
        self.panel2 = wx.Panel(self.panel, id=-1, style=wx.BORDER_SIMPLE)
        self.panel2.SetBackgroundColour("#fefed9")	
        self.text3 = wx.StaticText(self.panel2, id=-1, label="",
                                   style=wx.ALIGN_RIGHT | wx.ST_NO_AUTORESIZE)
        self.text3.SetForegroundColour("#68b1d8")
        
        self.text4 = wx.StaticText(self.panel, id=-1, label=_("Automatic search :"))
        font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
        font.SetWeight(wx.BOLD)
        self.text4.SetFont(font)
        self.text4.SetForegroundColour("#646464")
        
        self.text5 = wx.StaticText(self.panel, id=-1, label=_("Input :  "))
        
    #-----------------------------------------------------------------------

    def create_list_ctrl(self):
        # Create a BitmapTextButton for ListCtrlFolders
        bitmapDir = os.path.join(appDir, "bitmaps")
        bmp = wx.Bitmap(os.path.join(bitmapDir, "btn_Folders.png"))
        self.btnFolders = GenBitmapTextButton(self.panel, id=-1,
                                              size=(200, -1),
                                              bitmap=bmp,
                                              label=_(" Sources List "))
        # or PyEmbeddedImage
        # bmp = images.Btn_Folders.GetBitmap()
        # self.btnFolders = GenBitmapTextButton(self.panel, id=-1,
        #                                       size=(200, -1),
        #                                       bitmap=bmp,
        #                                       label=_(" Sources List "))
        font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
        font.SetWeight(wx.BOLD)
        self.btnFolders.SetFont(font)        
        self.btnFolders.SetBezelWidth(2)
        
        self.btnFolders.SetBackgroundColour("#4f4f4f")
        self.btnFolders.SetForegroundColour(wx.WHITE)
        
        # Bind some events to an events handler
        self.Bind(wx.EVT_BUTTON, self.OnFolders, self.btnFolders)
        self.btnFolders.Bind(wx.EVT_ENTER_WINDOW, self.on_btn_enter)
        self.btnFolders.Bind(wx.EVT_ENTER_WINDOW, self.on_folders_info, id=-1)
        self.btnFolders.Bind(wx.EVT_LEAVE_WINDOW, self.on_btn_leave)
        
        #----------
        
        # if wx.Platform == "__WXMAC__":
        #     wx.SystemOptions.SetOptionInt("mac.listctrl.always_use_generic", 0)
        
        self.listFolders = list_ctrl_virtual.My_ListCtrlFolders(self.panel, self)
        
        #-------------------------------------------------------------------
        
        # Create a BitmapTextButton for ListCtrlSupports
        bitmapDir = os.path.join(appDir, "bitmaps")
        bmp = wx.Bitmap(os.path.join(bitmapDir, "btn_Supports.png"))
        self.btnSupports = GenBitmapTextButton(self.panel, id=-1,
                                              size=(200, -1),
                                              bitmap=bmp,
                                              label=_(" Supports List "))
        # or PyEmbeddedImage
        # bmp = images.Btn_Supports.GetBitmap()
        # self.btnSupports = GenBitmapTextButton(self.panel, id=-1,
        #                                        size=(200, -1),
        #                                        bitmap=bmp,
        #                                        label=_(" Supports List "))
        font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
        font.SetWeight(wx.BOLD)
        self.btnSupports.SetFont(font)        
        self.btnSupports.SetBezelWidth(2)
        
        self.btnSupports.SetBackgroundColour("#4f4f4f")
        self.btnSupports.SetForegroundColour(wx.WHITE)
        
        # Bind some events to an events handler
        self.Bind(wx.EVT_BUTTON, self.OnSupports, self.btnSupports)
        self.btnSupports.Bind(wx.EVT_ENTER_WINDOW, self.on_btn_enter)
        self.btnSupports.Bind(wx.EVT_ENTER_WINDOW, self.on_supports_info, id=-1)
        self.btnSupports.Bind(wx.EVT_LEAVE_WINDOW, self.on_btn_leave)
        
        #----------
        
        # if wx.Platform == "__WXMAC__":
        #     wx.SystemOptions.SetOptionInt("mac.listctrl.always_use_generic", 0)
        
        self.listSupports = list_ctrl_virtual.My_ListCtrlSupports(self.panel, self)

    #-----------------------------------------------------------------------

    def populate_list_ctrl(self):
        self.SelectedSupport = ""
        if self.dbObject:
            self.listFolders.SetMyRows(self.dbObject.RetrieveRecords("Dossiers"))   ######
            self.listFolders.SpecialRefresh()            
            self.listSupports.SetMyRows(self.dbObject.RetrieveRecords("Supports"))   ######
            self.listSupports.SpecialRefresh()  		

    #-----------------------------------------------------------------------
        
    def create_search_ctrl(self):
        self.searchCtrl = search_ctrl.My_SearchCtrl(self.panel, self)
       
        # Bind an enter window events to an events handler
        self.searchCtrl.Bind(wx.EVT_ENTER_WINDOW, self.on_search_info, id=-1)
        self.Bind(wx.EVT_SIZE, self.on_resize)
       
    #-----------------------------------------------------------------------

    def create_print_data(self):
        self.printData = wx.PrintData()
        self.printData.SetPaperId(wx.PAPER_A4)
        self.printData.SetPrintMode(wx.PRINT_MODE_PRINTER)
        self.printData.SetOrientation(wx.PORTRAIT)
        self.printData.SetQuality(wx.PRINT_QUALITY_HIGH)
        self.printData.SetColour(False)
        # self.printData.SetPrinterName("")    # Printer Name 
        # self.printData.SetNoCopies(30)       # Copies Number 
        # self.printData.SetCollate(True)      # Combined Files 
            
        self.canvas = scrolled_window.My_Canvas(self.panel, size=(0, 0))

    #-----------------------------------------------------------------------
        
    def create_btn_bar(self):
        self.btnSave = wx.Button(self.panel, id=wx.ID_SAVE, label="", size=(90, -1))
        self.btnPrint = wx.Button(self.panel, id=wx.ID_PRINT, label="", size=(90, -1))
        self.btnDelete = wx.Button(self.panel, id=wx.ID_DELETE, label="", size=(90, -1))
        self.btnAbout = wx.Button(self.panel, id=wx.ID_ABOUT, label="", size=(90, -1))
        self.btnQuit = wx.Button(self.panel, id=wx.ID_EXIT, label="", size=(90, -1))
        
        # Bind some buttons events to an events handler
        self.Bind(wx.EVT_BUTTON, self.on_save, self.btnSave)
        self.Bind(wx.EVT_BUTTON, self.on_print, self.btnPrint)
        self.Bind(wx.EVT_BUTTON, self.on_delete, self.btnDelete)
        self.Bind(wx.EVT_BUTTON, self.on_about, self.btnAbout)
        self.Bind(wx.EVT_BUTTON, self.on_close, self.btnQuit)
        
        # Bind some enter window events to an events handler
        self.btnSave.Bind(wx.EVT_ENTER_WINDOW, self.on_save_info)
        self.btnPrint.Bind(wx.EVT_ENTER_WINDOW, self.on_print_info)
        self.btnDelete.Bind(wx.EVT_ENTER_WINDOW, self.on_delete_info)
        self.btnAbout.Bind(wx.EVT_ENTER_WINDOW, self.on_about_info)
        self.btnQuit.Bind(wx.EVT_ENTER_WINDOW, self.on_close_info)
        
        # Bind the events size to an events handler
        self.Bind(wx.EVT_SIZE, self.on_resize)
        
    #-----------------------------------------------------------------------
        
    def create_image(self):
        bitmapDir = os.path.join(appDir, "bitmaps")
        bmp = wx.Bitmap(os.path.join(bitmapDir, "icons.png"))
        # or PyEmbeddedImage
        # bmp = images.Icons.GetBitmap()
        self.img = wx.StaticBitmap(self.panel, -1)
        self.img.SetBitmap(bmp)
        self.img.SetToolTipString(_("Cool !"))
        
        # Bind the size events to an events handler
        self.Bind(wx.EVT_SIZE, self.on_resize)
        
    #-----------------------------------------------------------------------
        
    def do_layout(self):
        txt1Sizer = wx.BoxSizer(wx.HORIZONTAL)
        
        btnFoldersSizer = wx.BoxSizer(wx.HORIZONTAL)
        listFoldersSizer = wx.BoxSizer(wx.VERTICAL)
        txt2Sizer = wx.BoxSizer(wx.HORIZONTAL)
        vbox2 = wx.BoxSizer(wx.VERTICAL)
        
        btnSupportsSizer = wx.BoxSizer(wx.HORIZONTAL)
        listSupportsSizer = wx.BoxSizer(wx.VERTICAL)
        txt3Sizer = wx.BoxSizer(wx.HORIZONTAL)
        vbox3 = wx.BoxSizer(wx.VERTICAL)
        
        txt4Sizer = wx.BoxSizer(wx.HORIZONTAL)
        txtCtrlSizer = wx.BoxSizer(wx.HORIZONTAL)
        btnSizer = wx.BoxSizer(wx.VERTICAL)
        
        btnQuitSizer = wx.BoxSizer(wx.HORIZONTAL)
        
        #--------------

        # wx.BoxSizer(window, proportion, flag, border)        
        # wx.BoxSizer(sizer, proportion, flag, border)        
        txt1Sizer.Add(self.text1, 1, wx.EXPAND, 0)
        
        btnFoldersSizer.Add(self.btnFolders, 1, wx.EXPAND | wx.BOTTOM, 2)
        txt2Sizer.Add(self.text2, 1, wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL, 2)
        vbox2.Add(txt2Sizer, 1, wx.ALIGN_CENTRE | wx.EXPAND | wx.ALL, 2)
        
        listFoldersSizer.Add(btnFoldersSizer, 0, wx.EXPAND, 0)
        listFoldersSizer.Add(self.listFolders, 1, wx.EXPAND, 0)
        listFoldersSizer.Add(self.panel1, 0, wx.ALL | wx.EXPAND, 0)

        btnSupportsSizer.Add(self.btnSupports, 1, wx.EXPAND | wx.BOTTOM, 2)
        txt3Sizer.Add(self.text3, 1, wx.ALIGN_RIGHT | wx.EXPAND | wx.ALL, 2)
        vbox3.Add(txt3Sizer, 1, wx.ALIGN_CENTRE | wx.EXPAND | wx.ALL, 2)
        
        listSupportsSizer.Add(btnSupportsSizer, 0, wx.EXPAND, 0)
        listSupportsSizer.Add(self.listSupports, 1, wx.EXPAND, 0)
        listSupportsSizer.Add(self.panel2, 0, wx.ALL | wx.EXPAND, 0)
        
        txt4Sizer.Add(self.text4, 0, wx.EXPAND, 0)
        txtCtrlSizer.Add(self.text5, 1, wx.EXPAND | wx.LEFT, 20)
        txtCtrlSizer.Add(self.searchCtrl, 0, wx.EXPAND, 0)
        
        btnSizer.Add(self.img, 0, wx.LEFT | wx.BOTTOM |
                     wx.ALIGN_CENTER_HORIZONTAL, 10)
        btnSizer.Add(self.btnSave, 0, wx.LEFT | wx.BOTTOM |
                     wx.ALIGN_CENTER_HORIZONTAL, 10)
        btnSizer.Add(self.btnPrint, 0, wx.LEFT | wx.BOTTOM |
                     wx.ALIGN_CENTER_HORIZONTAL, 10)
        btnSizer.Add(self.btnDelete, 0, wx.LEFT | wx.BOTTOM |
                     wx.ALIGN_CENTER_HORIZONTAL, 10)
        btnSizer.Add(self.btnAbout, 0, wx.LEFT | wx.BOTTOM |
                     wx.ALIGN_CENTER_HORIZONTAL, 10)
        
        btnQuitSizer.Add(self.btnQuit, 0, wx.EXPAND, 0)
        
        #--------------
        #--------------
        
        topSizer = wx.BoxSizer(wx.HORIZONTAL)
        
        #--------------
        
        topSizer.Add(listFoldersSizer, 1, wx.EXPAND | wx.RIGHT, 6)
        topSizer.Add(listSupportsSizer, 1, wx.EXPAND | wx.LEFT, 6)
        topSizer.Add(btnSizer, 0, wx.EXPAND, 0)
        
        #--------------
        #--------------
        
        allSizer = wx.BoxSizer(wx.VERTICAL)
        
        #--------------
        
        allSizer.Add(txt1Sizer, 0, wx.EXPAND | wx.BOTTOM, 10)
        allSizer.Add(topSizer, 1, wx.EXPAND, 0)

        allSizer.Add(txt4Sizer, 0, wx.EXPAND | wx.LEFT | wx.TOP, 13)

        allSizer.Add(txtCtrlSizer, 0, wx.LEFT | wx.TOP, 13)
        allSizer.Add(btnQuitSizer, 0, wx.ALIGN_RIGHT)
        
        #--------------
        #--------------
        
        borderSizer = wx.BoxSizer(wx.VERTICAL)
        
        #--------------
        
        borderSizer.Add(allSizer, 1, wx.EXPAND | wx.ALIGN_RIGHT | wx.ALL , 13)
        
        #--------------
        #--------------

        self.panel1.SetAutoLayout(True)
        self.panel1.SetSizer(vbox2)

        self.panel2.SetAutoLayout(True)
        self.panel2.SetSizer(vbox3)

        self.panel.SetAutoLayout(True)
        self.panel.SetSizer(borderSizer)
        borderSizer.Fit(self.panel)
        
    #-----------------------------------------------------------------------

    # StatusBar help        
    def on_panel_info(self, event):
        self.sb.SetStatusText(text=_("Welcome to PyArchiver !"))
        event.Skip()
        
    def on_folders_info(self, event):
        self.sb.SetStatusText(text=_("Displays all sources of the database."))
        event.Skip()
        
    def on_supports_info(self, event):
        self.sb.SetStatusText(text=_("Displays all supports of the database."))
        event.Skip()
        
    def on_save_info(self, event):
        self.sb.SetStatusText(text=_("Save source in the database."))
        event.Skip()
        
    def on_print_info(self, event):
        self.sb.SetStatusText(text=_("Print the content of support selected."))
        event.Skip()
        
    def on_delete_info(self, event):
        self.sb.SetStatusText(text=_("Removes source or support of the database."))
        event.Skip()
        
    def on_about_info(self, event):
        self.sb.SetStatusText(text=_("About this application."))
        event.Skip()
        
    def on_close_info(self, event):
        self.sb.SetStatusText(text=_("Quit this application."))
        event.Skip()
        
    def on_search_info(self, event):
        self.sb.SetStatusText(text=_("Search source of the data base (validate with the Enter key)."))
        event.Skip()
        
    #-----------------------------------------------------------------------

    def on_btn_enter(self, event):
        self.obj = event.GetEventObject()
        self.obj.SetBackgroundColour("GRAY")
        self.obj.Refresh()
        
    def on_btn_leave(self, event):
        self.obj = event.GetEventObject()
        self.obj.SetBackgroundColour("#4f4f4f")
        self.obj.Refresh()
        
    #-----------------------------------------------------------------------

    def on_roll(self, event):        
        # Set the flag to the state we want regardless of whether or not 
        #it's in currently in the opposite state.
        self.roll_unroll(wantToRoll=True)

        self.GetTitle()
        self.SetTitle(_("PyArchiver v%s - F4 key for UnRoll")
                      % version_info.APPLICATIONVERSION_STRING)
        
  
    def on_unroll(self, event):        
        # Set the flag to the state we want regardless of whether or not 
        #it's in currently in the opposite state.
        self.roll_unroll(wantToRoll=False)

        self.GetTitle()
        self.SetTitle(_("PyArchiver v%s")
                      % version_info.APPLICATIONVERSION_STRING)
        

    def roll_unroll(self, wantToRoll):        
        # Save the current size only if the Frame is not rolled up.
        if not bool(self.isRolled) :
            self.unrolledFrameClientSize_size = self.GetClientSize()
            print ""
            print "RollUnRoll() : not bool(self.isRolled) : ClientSize is ", self.unrolledFrameClientSize_size
        
        print "RollUnRolled() : self.isRolled", self.isRolled
        print "RollUnRolled() : self.unrolledFrameClientSize_size : ", self.unrolledFrameClientSize_size
        print "RollUnRolled() : self.SetClientSize(self.unrolledFrameClientSize_size)"
        
        if bool(wantToRoll):   # UnRoll            
            print "RollUnRolled() : Rolling..."
            
            self.SetClientSize((self.unrolledFrameClientSize_size[0], 0))            
            self.isRolled = True    # Set to match this new state.
        
        else :      # Roll            
            print "RollUnRolled() : UnRolling..."
            
            self.SetClientSize(self.unrolledFrameClientSize_size)            
            self.isRolled = False   # Set to match this new state.
        
    #----------------------------------------------------------------------
        
    def on_switch_lang_fr(self):
        wx.GetApp().set_lang("fr")


    def on_switch_lang_en(self):
        wx.GetApp().set_lang("en")
        
    #-----------------------------------------------------------------------

    def verify_password (self, password):
        """ Check the input password """

        hashHardcoded = "f4353bd17732e54b6b120e628b211e78c279e330"
        hash = hashlib.sha1()
        hash.update(password)
        if hash.hexdigest() == hashHardcoded:
            result = True
        else:
            result = False	
        return result		

    #-----------------------------------------------------------------------
    
    def FilterSupports(self, item_text):
        self.listSupports.SetMyRows(self.dbObject.FilterField("dossiers", item_text, "supports"))
        self.listSupports.SpecialRefresh()  
        self.SelectedSupport = ""
        self.SelectedFolder = ""		

    #-----------------------------------------------------------------------
        
    def FilterFolders(self, item_text):
        self.listFolders.SetMyRows(self.dbObject.FilterField("supports", item_text, "dossiers"))
        self.listFolders.SpecialRefresh() 
        self.SelectedSupport = ""
        self.SelectedFolder = "" 		

    #-----------------------------------------------------------------------
            
    def OpenDrive (self, drivename):
        """ Lister tous les dossiers localises sur le lecteur de cdrom """
        
        try:
            print drivename
            FileList = os.listdir(drivename)
            return FileList
        except os.error:
            onerror(os.listdir, path, sys.exc_info())
            print _("The drive %s is inaccessible.") % drivename
            return False

    #-----------------------------------------------------------------------

    def RetrieveDrivePath(self):
        """ Retrouver l'emplacement du lecteur de cdrom """

        config_Location = os.path.join(appDir, "config")
        configSource_File = os.path.join(config_Location, "Source.cfg")
        
        config = ConfigParser.ConfigParser()
        config.read(configSource_File)        
        return config.get("SOURCE", "drive1")
        #return config.get("Drives", "drive1")
    
    #-----------------------------------------------------------------------
        
    def on_save(self, event):
        """ Enregistrer un support dans la base de donnees """

        if self.dbObject == None:
            return False

        self.saveDlg = warning_dialog.My_WarningDialog(self,
                                                      message=_("Make sure you have \ninserted a CD/DVD in the drive \nbefore start saving !"),
                                                      title=_("Warning"),
                                                      pos=(-1, -1))
        
        self.saveDialog = save_dialog.My_SaveDialog(self,
                                                   title=_("Save a support"),
                                                   pos=(-1, -1))

        result = False
        if self.saveDialog.PressedButton() == True:
            ContentOfDrive = self.OpenDrive(self.RetrieveDrivePath())
            ThisSupport = self.saveDialog.GetValue()
            if ThisSupport != "":
                if ContentOfDrive:
                    self.dbObject.FillDataBase(self.RetrieveDrivePath(), ContentOfDrive, ThisSupport)
                self.listFolders.SetMyRows(self.dbObject.RetrieveRecords("dossiers"))
                self.listFolders.SpecialRefresh()            
                self.listSupports.SetMyRows(self.dbObject.RetrieveRecords("supports"))
                self.listSupports.SpecialRefresh() 
                result = True
            else:
                print "Support must have a name !"
        self.saveDialog.Destroy()
        return result

    #----------------------------------------------------------------------
        
    def on_print(self, event):
        """ Visualiser et imprimer une jaquette """

        self.printDlg = warning_dialog.My_WarningDialog(self,
                                                       message=_("Make sure you have \nselected a Support before \nto launch the printing !"),
                                                       title=_("Warning"),
                                                       pos=(-1, -1))
            
        if wx.Platform == "__WXGTK__":
            self.pdd = wx.PrintDialogData(self.printData)
            self.pdd.SetPrintToFile(True)      # Printed to file 
            # self.pdd.SetPrintData(self.printData)
            # self.pdd.SetMinPage(2)
            # self.pdd.SetMaxPage(20)
            # self.pdd.SetFromPage(1)          # Pages series (from x - to y)
            # self.pdd.SetToPage(2)

            printer = wx.Printer(self.pdd)
            printout = print_framework.My_Printout(self.canvas, _("  - Jacket"))
            printer.Print(self, printout, True)

            
        else:
            self.pdd = wx.PrintDialogData(self.printData)
            # self.pdd.SetPrintData(self.printData)
            # self.pdd.SetMinPage(2)
            # self.pdd.SetMaxPage(20)
            # self.pdd.SetFromPage(1)          # Pages series (from x - to y)
            # self.pdd.SetToPage(2)
            # self.pdd.SetPrintToFile(True)    # Printed to file 
            
            printout = print_framework.My_Printout(self.canvas, _("  - Jacket"))
            printout2 = print_framework.My_Printout(self.canvas, _("  - Jacket"))
            self.preview = wx.PrintPreview(printout, printout2, self.pdd)

            if not self.preview.Ok():
                wx.MessageBox(_("There was a problem printing.\nPerhaps your current printer is \nnot set correctly ?"),
                              _("Printing"), wx.OK)
                return
        
            # Initial zoom value
            if wx.Platform == "__WXMAC__":
                self.preview.SetZoom(55)
            else:
                self.preview.SetZoom(40) 
        
            pfrm = wx.PreviewFrame(self.preview, None,
                                   _("Preview"))

            iconDir = os.path.join(appDir, "icons")
            frameicon = wx.Icon(os.path.join(iconDir, "printing.ico"), wx.BITMAP_TYPE_ICO)
            # or PyEmbeddedImage
            # frameicon = images.Printing.GetIcon() 
            pfrm.SetIcon(frameicon)
            
            pfrm.Initialize()
        
            pfrm.SetPosition(self.GetPosition())
            pfrm.SetSize(self.GetSize())
            # pfrm.Maximize()
            pfrm.Show(True)
           
    #----------------------------------------------------------------------
        
    def on_delete(self, event):
        """ Supprimer un support de la base de donnees """

        self.deleteDlg = warning_dialog.My_WarningDialog(self,
                                                        message=_("Make sure you have \ninserted a Support before \nactivate the deleting !"),
                                                        title=_("Warning"),
                                                        pos=(-1, -1))
        
        self.pwdDialog = password_dialog.My_PasswordDialog(self,
                                                          title=_("Delete an entry"),
                                                          pos=(-1, -1))

        if self.verify_password(self.pwdDialog.getpassword()):
            if  self.SelectedSupport != "":
                print self.SelectedSupport
                self.dbObject.DeleteSupport(self.SelectedSupport)
                self.listFolders.SetMyRows(self.dbObject.RetrieveRecords("dossiers"))
                self.listSupports.SetMyRows(self.dbObject.RetrieveRecords("supports"))
                self.listSupports.SpecialRefresh()
                self.listFolders.SpecialRefresh()
                self.SelectedSupport = ""

            elif self.SelectedFolder != "":
                print self.SelectedFolder
                self.dbObject.DeleteFolder(self.SelectedFolder)
                self.listFolders.SetMyRows(self.dbObject.RetrieveRecords("dossiers"))
                self.listSupports.SetMyRows(self.dbObject.RetrieveRecords("supports"))
                self.listSupports.SpecialRefresh()
                self.listFolders.SpecialRefresh()
                self.SelectedFolder = ""
        
    #-----------------------------------------------------------------------
        
    def on_about(self, event):
        self.about = about_notebook.My_AboutNotebook(self)
        
    #-----------------------------------------------------------------------
        
    def on_memento(self, event):
        self.memento = memento_provider.My_Memento(self)
        
    #-----------------------------------------------------------------------
        
    def on_setting(self, event):
        self.setting = setting_dialog.My_SettingDialog(self)
        
    #-----------------------------------------------------------------------
    def on_shell_window(self, event):
        if self.shell:
            # If it already exists then just make sure it's visible
            s = self.shell
            if s.IsIconized():
                s.Iconize(False)
            s.Raise()
        else:
            # Make a PyShell window
            from wx import py
            namespace = { 'wx'    : wx,
                          'app'   : wx.GetApp(),
                          'frame' : self,
                          }
            self.shell = py.shell.ShellFrame(None, locals=namespace)
            self.shell.SetSize((640,480))
            self.shell.Show()
            print ""
            print "Display the PyShell frame"
        
            # Hook the close event of the main frame window so that we
            # close the shell at the same time if it still exists            
            def CloseShell(event):
                if self.shell:
                    self.shell.Close()
                event.Skip()
            self.Bind(wx.EVT_CLOSE, CloseShell)

    #-----------------------------------------------------------------------
            
    def on_widget_inspector(self, event):
        # Activate the widget inspection tool
        from wx.lib.inspection import InspectionTool
        if not InspectionTool().initialized:
            InspectionTool().Init()

        # Find a widget to be selected in the tree.  Use either the
        # one under the cursor, if any, or this frame.
        wnd = wx.FindWindowAtPointer()
        if not wnd:
            wnd = self
        InspectionTool().Show(wnd, True)
        print ""
        print "Display the wxPython Widget Inspection Tool frame"
        
    #-----------------------------------------------------------------------
        
    def on_resize(self, event):
        self.img.Refresh(), self.panel1.Refresh(),
        self.panel2.Refresh(), self.searchCtrl.Refresh()
        event.Skip()

    #-----------------------------------------------------------------------
    
    def OnFolders(self, event):
        """ Lister tous les dossiers de la base """        

        if self.dbObject:
            self.listFolders.SetMyRows(self.dbObject.RetrieveRecords("dossiers"))
            self.listFolders.SpecialRefresh()            
 
    #----------------------------------------------------------------------

    def OnSupports(self, event):
        """ Lister tous les supports de la base """        

        if self.dbObject:
            self.listSupports.SetMyRows(self.dbObject.RetrieveRecords("supports"))
            self.listSupports.SpecialRefresh()
            
    #-----------------------------------------------------------------------

    def on_erase_background(self, event):
        dc = event.GetDC()
        if not dc:
            dc = wx.ClientDC(self)
            rect = self.GetUpdateRegion().GetBox()
            dc.SetClippingRect(rect)

    #-----------------------------------------------------------------------
            
    def on_iconfiy(self, event):
        event.Skip()

    #-----------------------------------------------------------------------
        
    def on_maximize(self, event):
        event.Skip()

    #-----------------------------------------------------------------------

    def save_state(self, event):
        """Save position and size"""

        position = self.GetPosition()
        size = self.GetSize()

        cfg = wx.Config(appName="SizeAndPos")
        
        cfg.Write("pos", repr(position.Get()))
        cfg.Write("size", repr(size.Get()))        
        event.Skip()
        print ""
        print "Save frame position : %s" % (position)
        print "Save frame size : %s" % (size)        
       
      
    def restore_state(self):
        """Restore the saved position and size"""
       
        cfg = wx.Config(appName="SizeAndPos")
        
        position = cfg.Read("pos")
        size = cfg.Read("size")
        # Turn strings back into tuples
        position = eval(position)
        size = eval(size)
        # Restore settings to Frame
        self.SetPosition(position)
        self.SetSize(size)

    #-----------------------------------------------------------------------
        
    def on_close(self, event):
        self.Close(True)
        
    #-----------------------------------------------------------------------
        
    def on_close_window(self, event):
        """ Quit this application """

        dlg = wx.MessageDialog(self,
                               message=_("\nDo you really quit this application ?"),
                               caption=_("Quit"),
                               style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)

        if dlg.ShowModal() == wx.ID_YES:
            if self.tskic is not None:
                self.tskic.Destroy()

            self.sb.timer.Stop()
            del self.sb.timer
            event.Skip()
            self.Destroy()
        else:
            dlg.Destroy()
